﻿namespace LeRenard.ScriptWars.Model.GridSystem
{
    /// <summary>
    /// Makes a grid completely null or as default value.
    /// </summary>
    /// <typeparam name="TValue"></typeparam>
    public class DefaultGridInitializer<TValue> : IGridInitializer<TValue>
        where TValue : class
    {
        /// <summary>
        /// Gets the default value to be used.
        /// </summary>
        public TValue DefaultValue { get { return _defaultValue; } }
        private readonly TValue _defaultValue;

        /// <summary>
        /// Creates a new <see cref="DefaultGridInitializer"/> object.
        /// </summary>        
        public DefaultGridInitializer(TValue defaultValue)
        {
        }
        /// <summary>
        /// Creates a new <see cref="DefaultGridInitializer"/> object.
        /// </summary>
        public DefaultGridInitializer() : this(default(TValue)) { }

        /// <summary>
        /// Initializes the grid based on the implemented strategy.
        /// </summary>        
        public GridCell<TValue>[,] Initialize(IGrid<TValue> grid)
        {
            var layout = new GridCell<TValue>[grid.Size.Height, grid.Size.Width];

            for (int y = 0; y < grid.Size.Height; y++) {
                for (int x = 0; x < grid.Size.Width; x++) {
                    layout[y, x] = new GridCell<TValue>(GenerateValue());
                }
            }

            return layout;
        }

        /// <summary>
        /// Generates a value for a <see cref="GridCell"/>.
        /// </summary>        
        public virtual TValue GenerateValue()
        {
            return DefaultValue;
        }
    }
}
